//
//  TTDetailFliterCustomView.m
//  TaoTuo
//
//  Created by admin on 2019/12/10.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDetailFliterCustomView.h"
#import "TTDetailFilterCustomHeaderView.h"

static NSString *cellIdentifier = @"detail_filter_custom_cell_id";

@interface TTDetailFliterCustomView ()

@property (nonatomic, strong) TTDetailModel *model;
@property (nonatomic, strong) TTDetailFilterCustomHeaderView *headerView;
@property (nonatomic, copy) NSString *styleLibraryId;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableDictionary *dic;
@property (nonatomic, strong) NSArray *tempArray;

@end

@implementation TTDetailFliterCustomView

- (instancetype)initWithFrame:(CGRect)frame model:(nonnull TTDetailModel *)model styleLibraryId:(nonnull NSString *)styleLibraryId selectArray:(nonnull NSArray *)selectArray
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.styleLibraryId = styleLibraryId;
        self.page = 1;
        
        self.model = model;
        self.tempArray = selectArray;
        
        [self setupSubviews];
        
//        [self getNetworkData];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTDetailStockListModel *model = self.dataArray[indexPath.row];
    TTSpecificationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.delegate = self;
    [cell setModel:model specificationCellType:CellTypeWithSpecificationChoose categoryName:self.model.CategoryName];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTDetailStockListModel *model = self.dataArray[indexPath.row];
    model.selected = !model.selected;
    
    if (model.selected) {
        if (![self.selectArray containsObject:model]) {
            [self.selectArray addObject:model];
        }
    } else {
        [self.selectArray removeObject:model];
    }
    
    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTDetailStockListModel *model = self.dataArray[indexPath.row];
    return model.h;
}


#pragma mark - MGSwipeTableCellDelegate
- (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction fromPoint:(CGPoint)point
{
    return NO;
}

///请求数据
- (void)getNetworkData
{
    NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:self.dic];
    [paramsDic setObject:@(self.page) forKey:@"param.pageIndex"];
    [paramsDic setObject:@(20) forKey:@"param.pageSize"];
    [paramsDic setObject:self.styleLibraryId forKey:@"param.styleLibraryId"];
    
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:getCustomList_api parameters:paramsDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        NSArray *arr = responseObject;
        
        [self.selectArray removeAllObjects];
        
        for (NSDictionary *dic in arr) {
            TTDetailStockListModel *model = [TTDetailStockListModel modelWithJSON:dic];
            
            model.buyCount = model.BuyNumberMin;
            
            for (TTDetailStockListModel *sModel in self.tempArray) {
                if ([model.GoodsBarCode isEqualToString:sModel.GoodsBarCode]) {
                    model.buyCount = sModel.buyCount;
                    model.selected = YES;
                    [self.selectArray addObject:model];
                }
            }
            
            [self setCellContentWithModel:model];
            [self.dataArray addObject:model];
        }
        
        if (arr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        @strongify(self)
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } showLoadingInView:self.tableView];
}

- (void)setCellContentWithModel:(TTDetailStockListModel *)model
{
    // 行间距
    NSString *contentStr = [model.GoodsAttr componentsJoinedByString:@"\n"];
    NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;
    [contentAttr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, contentStr.length)];
    CGFloat contentW = SCREEN_WIDTH - 15;
    CGFloat h = [contentStr boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style} context:nil].size.height + 20 + 20 + 20;
    model.h = h;
    
}

- (void)setupSubviews
{
    CGFloat h = self.height;
    
    [self addSubview:self.headerView];
    
    self.tableView.frame = CGRectMake(0, self.headerView.bottom, self.width, h - self.headerView.height);
    [self addSubview:self.tableView];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self getNetworkData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page++;
        [self getNetworkData];
    }];
    
    
}



- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.emptyImageName = @"nodata";
        _tableView.emptyTitel = @"暂无数据";
        [_tableView registerClass:[TTSpecificationViewCell class] forCellReuseIdentifier:cellIdentifier];
    }
    return _tableView;
}

- (TTDetailFilterCustomHeaderView *)headerView
{
    if (!_headerView) {
        @weakify(self)
        _headerView = [TTDetailFilterCustomHeaderView customHeaderViewWithCustomizedModel:self.model.Customized selectFinished:^(NSMutableDictionary * _Nullable dic) {
            @strongify(self)
            NSLog(@">>> dic = %@", dic);
            self.dic = dic;
            self.page = 1;
            [self getNetworkData];
        }];
        [self addSubview:_headerView];
    }
    return _headerView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)selectArray
{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}





@end
